#
# \file
# \brief
# \author Marco Antonio Pinto Orellana
#
# -------------------------------------------------------------------------
#
# Copyright (C) 2012  (see AUTHORS file for a list of contributors)
# http://opentonitruum.sourceforge.net
#
# This file is part of OpenTonitruum - a simulator/library of signal trans-
# mission, modulation and processing in a wireless environment.
#
# OpenTonitruum is free software: you can redistribute it and/or modify it 
# under the terms of the Lesser GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at 
# your option) any later version.
#
# OpenTonitruum is distributed in the hope that it will be useful, but WITH-
# OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with OpenTonitruum.  If not, see <http://www.gnu.org/licenses/>.
#
# -------------------------------------------------------------------------
#

from SimPy.Simulation import *
from OpenTonitruum.Base.DataStructure import *
from HDLC import *
from PhysicalLayer import *

class LinkLayer(object):
	def __init__(self, id, protocol, physicalLayer):
		self.id = id
		self.medium = physicalLayer
		self.receivedData = Event()
		if protocol == "HDLC":
			self.protocol = HDLCProtocol(id = self.id, medium = self.medium, rq = HDLCRQ.Inactive, type = HDLCTypeEquipment.Secondary)
			self.protocol.receivedData.add(self.onReceived)
		else:
			raise Exception("Protocol " + protocol + " not supported")
	
	def send(self, destination, data):
		'''Method called from upper layer'''
		self.protocol.send(destination, data)
	
	def onReceived(self, frame, informationField):
		'''Method called from lower layer'''
		self.receivedData(frame, informationField)
		

